package com.financing.controller.system;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.financing.common.CONST;
import com.financing.components.uploader.Uploader;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;

@Clear
public class SystemController extends Controller {

	/**
	 * 文件上传
	 * 
	 * @throws Exception
	 */
	public void uploadFile() throws Exception {
		String action = getPara("action","uploadimage");
		if(Str.eq("config", action)) {
			InputStream is = null;
			try {
				is = new FileInputStream(new File(CONST.ROOT + "/WEB-INF/classes/config.json"));
				StringBuffer sb = new StringBuffer();
				byte[] b = new byte[1024];
				int length = 0;
				while (-1 != (length = is.read(b))) {
					sb.append(new String(b, 0, length, "utf-8"));
				}
				String result = sb.toString().replaceAll("/\\*(.|[\\r\\n])*?\\*/", "");
				JSONObject json = JSONObject.parseObject(result);
				renderHtml(json.toJSONString());
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				is.close();
			}
		}else if(Str.eq("uploadimage", action)) {
			HttpServletRequest request = getRequest();
			Uploader up = new Uploader(request);
			up.setSavePath("upload");
			String[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp", ".apk", ".psd", ".xls", ".xlsx" };
			up.setAllowFiles(fileType);
			up.setMaxSize(50000); // 单位KB
			up.upload();
			String callback = getPara("callback");
			String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName()
					+ "\", \"size\": " + up.getSize() + ", \"state\": \"" + up.getState() + "\", \"type\": \""
					+ up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
			renderHtml(callback == null ? result : "<script>" + callback + "(" + result + ")</script>");
		}
	}
	
	/**
	 *  文件删除
	 * @throws Exception 
	 */ 
	public void removeFile() throws Exception{
	    String path = CONST.ROOT+"/attachment/upload/"+getPara("path"); 
	    String fileName = path;
        File file = new File(fileName);
        if (!file.exists() || !file.isFile()) {
    		renderJson(Json.ok("文件删除失败，原因:" + fileName + "不存在！"));
        } else {
            if (file.delete()) {
        		renderJson(Json.ok("文件删除成功！"));
            } else {
        		renderJson(Json.ok("文件删除失败！"));
            } 
        }
    }
	
	/**
	 * 获取地区数据
	 * @throws Exception 
	 */ 
	public void area() throws Exception {
		renderJson(PropKit.use("basic-data.txt").get("area"));
	}
	
	/**
	 * 无权限提示
	 * @throws Exception 
	 */ 
	public void unAuth() throws Exception {
		renderJson(Json.error("您无权限使用该功能！"));
	}
	
	/**
	 * 跳转到登陆接口提示
	 * @throws Exception 
	 */ 
	public void toLoign() throws Exception {
		renderJson(Json.error("您无权限使用该功能，请重新登陆！"));
	}
}
 